import message from './message'


// 函数防抖
export var debounce = function debounce(fn) {
  var interval = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 300;
  var timeout = null;
  return function () {
    var _arguments = arguments,
        _this = this;

    clearTimeout(timeout);
    timeout = setTimeout(function () {
      fn.apply(_this, _arguments);
    }, interval);
  };
};
export var buildParams = function buildParams(params) {
  if (!params) return '';
  var str = '';

  for (var key in params) {
    str += key + '=' + encodeURIComponent(params[key]) + "&";
  }

  str = str.substr(0, str.length - 1);
  return "?" + str;
}; // 获取url后指定字段的值

export var getQueryString = function getQueryString(field) {
  var href = decodeURIComponent(window.location.href);
  var reg = new RegExp('[?&]' + field + '=([^&]*)', 'i');
  var string = reg.exec(href);
  return string ? decodeURIComponent(string[1]) : null;
}; // 获取url后的所有参数

export var getQueryUrlSearch = function getQueryUrlSearch() {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  var params = vars.reduce(function (obj, item) {
    var pair = item.split("=");
    obj[pair[0]] = decodeURIComponent(pair[1]);
    return obj;
  }, {});
  return params;
}; // 序列号post请求参数

export var flatPostParam = function flatPostParam(obj) {
  var query = '',
      name,
      value,
      fullSubName,
      subName,
      subValue,
      innerObj,
      i;

  for (name in obj) {
    value = obj[name];

    if (value instanceof Array) {
      for (i = 0; i < value.length; ++i) {
        subValue = value[i];
        fullSubName = name + '[' + i + ']';
        innerObj = {};
        innerObj[fullSubName] = subValue;
        query += flatPostParam(innerObj) + '&';
      }
    } else if (value instanceof Object) {
      for (subName in value) {
        subValue = value[subName];
        fullSubName = name + '[' + subName + ']';
        innerObj = {};
        innerObj[fullSubName] = subValue;
        query += flatPostParam(innerObj) + '&';
      }
    } else if (value !== undefined && value !== null) {
      query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
    }
  }

  return query.length ? query.substr(0, query.length - 1) : query;
};

export const isFunction=(fn)=>{
  return Object.prototype.toString.call(fn)==='[object Function]'
}
// 获取url后的所有参数
export const getAllQuery = ()=>{
  var query = window.location.search.substring(1);
  var vars = query.split("&")
  let params = vars.reduce((obj,item)=>{
    var pair = item.split("=")
    obj[pair[0]] = decodeURIComponent(pair[1])
    return obj
  },{})
  return params
}
// 获取cookie
export const getCookie=(key)=>{//获取cookie方法
  /*获取cookie参数*/
  var getCookie = document.cookie.replace(/[ ]/g,"");  //获取cookie，并且将获得的cookie格式化，去掉空格字符
  var arrCookie = getCookie.split(";")  //将获得的cookie以"分号"为标识 将cookie保存到arrCookie的数组中
  var tips;  //声明变量tips
  for(var i=0;i<arrCookie.length;i++){   //使用for循环查找cookie中的tips变量
    var arr=arrCookie[i].split("=");   //将单条cookie用"等号"为标识，将单条cookie保存为arr数组
    if(key==arr[0]){  //匹配变量名称，其中arr[0]是指的cookie名称，如果该条变量为tips则执行判断语句中的赋值操作
      tips=arr[1];   //将cookie的值赋给变量tips
      break;   //终止for循环遍历
    } 
  }
  return tips;
}
// 生产url链接后的查询参数
export const buildUrlParams=(params)=>{
  if(!params) return ''
  var str = ''
  for(var key in params){
    str += key+'='+encodeURIComponent(params[key])+"&"
  }
  str = str.substr(0,str.length-1)
  return "?"+str
}

export const numberToPrice=(num, options = {
  showDecimals:2,// 格式化小数位
  showSymbol : true
})=>{
  const showSymbol = options.showSymbol;
  const showDecimals = options.showDecimals;
  // console.log(showDecimals)
  return num.toLocaleString('zh', {
    style: showSymbol ? 'currency' : undefined,
    currency: showSymbol ? 'CNY' : undefined,
    maximumFractionDigits: showDecimals,
  });
}


// 获得角度
export const getAngle = (angx, angy) => {
  return Math.atan2(angy, angx) * 180 / Math.PI;
}

// 根据起点终点返回方向 1向上 2向下 3向左 4向右 0未滑动
export const getDirection = (startx, starty, endx, endy) => {
  let angx = endx - startx;
  let angy = endy - starty;
  let result = 'no';
  // 如果滑动距离太短
  if (Math.abs(angx) < 2 && Math.abs(angy) < 2) {
    return result;
  }
  let angle = getAngle(angx, angy);
  if (angle >= -135 && angle <= -45) {
    result = 'up';
  } else if (angle > 45 && angle < 135) {
    result = 'down';
  } else if ((angle >= 135 && angle <= 180) || (angle >= -180 && angle < -135)) {
    result = 'left';
  } else if (angle >= -45 && angle <= 45) {
    result = 'right';
  }
  return result;
}

export const getOrderStatusText = (status)=>{
  let keys = {
    'key_1':'待支付',
    'key_2':'待发货',
    'key_4':'已发货',
    'key_8':'已收货',
    'key_16':'已取消',
    'key_32':'交易完成',
    'key_19':'交易关闭',
  }
  return keys[`key_${status}`]
}

export const getRefundStatusText = (status)=>{
  let keys = {
    'key_3':'退款中',
    'key_6':'已退款',
    'key_9':'售后驳回',
    'key_12':'退货中',
    'key_15':'退货驳回',
    'key_21':'同意退货',
    'key_24':'取消售后',

  }
  return keys[`key_${status}`]
}
export const beforeUpload=(file)=>{
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
  if (!isJpgOrPng) {
    message.error('你只能上传 JPG/PNG 类型的图片!')
  }
  const isLt2M = file.size / 1024 / 1024 < 10;
  if (!isLt2M) {
    message.error('上传图片不能大于2M！')
  }
  return isJpgOrPng && isLt2M;
}

export const beforeVideoUpload=(file)=>{
  console.log(file)
  const isVideo = file.type.indexOf('video')>-1
  if (!isVideo) {
    message.error('请上传视频类型的数据!')
  }
  const isLt2M = file.size / 1024 / 1024 < 50;
  if (!isLt2M) {
    message.error('上传图片不能大于100M！')
  }
  return isVideo && isLt2M;
}





